package edu.poly.cs9223.assginment1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class UserThread  implements Runnable{
	User user;
	int day;
	Lock lock;
	Logger logger = LoggerFactory.getLogger(UserThread.class);
	public UserThread(User user, int day) {
		this.user = user;
		this.day = day;
	}

	public UserThread(User user, Lock lock,int day) {
		this(user,day);
		this.lock = lock;
	}

	int busyPeriod = Main.SCALE_UP_DOWN_THRESHOLD_PERIOD * 4;
	int idlePeriod = Main.IDLE_THRESHOLD_PERIOD * 3;
	int notThatBusyPeriod = Main.SCALE_UP_DOWN_THRESHOLD_PERIOD * 2;

	private void day1() {
		try {
			
			logger.info("[{}]【Initialization】", user.getName());
			logger.debug("creating security group for {}",user.getName());
			Main.createSecurityGroup(user);// create a security group
			logger.debug("creating key pair for {}", user.getName());
			Main.createKeyPair(user);// create a keypair
			
			
			Main.setupElasticity(user);
			user.sshLogin();
			Main.setupOnDemand(user);

			logger.info(
					"Day1----------------{} is very busy now     (@_@)---------[This will last for {} seconds]-------------",
					user.getName(), busyPeriod);
			user.doSomethingMore(busyPeriod);
			System.out.println("----------------------");
			logger.info(
					"Day1----------------{} is working now but not that busy   (-_-)-----------[This will last for {} seconds]-----------",
					user.getName(), notThatBusyPeriod);
			user.doSomething(notThatBusyPeriod);
			System.out.println("----------------------");
			logger.info(
					"Day1----------------{} is taking a nap now  (-_-)zzzzz----[This will last for {} seconds]-----------------",
					user.getName(), idlePeriod);
			user.doNothing(idlePeriod);
			System.out.println("----------------------");
			logger.info(
					"Day1----------------{} wakes up             (*_*)!!!!!---------------------",
					user.getName());
			Main.resumeInstances(user);
			System.out.println("----------------------");
			
			logger.info(
					"Day1----------------{} is working now but not that busy   (-_-)-----------[This will last for {} seconds]-----------",
					user.getName(), notThatBusyPeriod);
			user.doNothing(notThatBusyPeriod);
			System.out.println("----------------------");
			logger.info(
					"Day1----------------{} is  getting of work              (^_^)----------------------",
					user.getName());
			Main.logout(user);
			System.out.println("----------------------");
			
			//update the lock value
			this.lock.setLockValue(this.lock.getLockValue()+1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void day2() {
		try {
			logger.info("Day2----start instance for [{}]", user.getName());
			Main.createNewInstance(user);
			Main.resumeAutoScaling(user);
			Main.resumeMonitoring(user);

			user.sshLogin();

			logger.info(
					"Day2----------------{} is very busy now     (@_@)---------[This will last for {} seconds]-------------",
					user.getName(), busyPeriod);
			user.doSomethingMore(busyPeriod);
			System.out.println("----------------------");
			
//			logger.info(
//					"Day2----------------{} is working now but not that busy   (-_-)-----------[This will last for {} seconds]-----------",
//					user.getName(), notThatBusyPeriod);
//			user.doSomething(notThatBusyPeriod);
//			
//			System.out.println("----------------------");
//			logger.info(
//					"Day2----------------{} is taking a nap now  (-_-)zzzzz----[This will last for {} seconds]-----------------",
//					user.getName(), idlePeriod);
//			user.doNothing(idlePeriod);
//			System.out.println("----------------------");
//			
//			logger.info(
//					"Day2----------------{} wakes up             (*_*)!!!!!---------------------",
//					user.getName());
//			Main.resumeInstances(user);
//			System.out.println("----------------------");
			
//			logger.info(
//					"Day2----------------{} is very busy now     (@_@)---------[This will last for {} seconds]-------------",
//					user.getName(), busyPeriod);
//			user.doSomethingMore(busyPeriod);
//			System.out.println("----------------------");
			
			logger.info(
					"Day2----------------{} is getting of work              (^_^)----------------------",
					user.getName());
			Main.logout(user);
			System.out.println("----------------------");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void test() throws InterruptedException
	{
		logger.info("wait{}",this);
		user.doNothing(10000);
		logger.info("wait-end{}",this);
	}

	public void run() {
		if (this.day == 1)
			day1();
		else if(this.day ==2)
			day2();
		else
			try {
				test();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
